package com.nuc.wxh.vaccine_appointment.vaccines.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuc.wxh.vaccine_appointment.base.exception.VaccineException;
import com.nuc.wxh.vaccine_appointment.base.listener.HospitalListener;
import com.nuc.wxh.vaccine_appointment.base.result.ResultCodeEnum;
import com.nuc.wxh.vaccine_appointment.base.util.JwtInfo;
import com.nuc.wxh.vaccine_appointment.base.util.JwtUtils;
import com.nuc.wxh.vaccine_appointment.base.util.MD5;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.AdminDoctor;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.AdminHospital;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.vo.AdminHospitalVo;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.vo.LoginVo;
import com.nuc.wxh.vaccine_appointment.vaccines.mapper.AdminHospitalMapper;
import com.nuc.wxh.vaccine_appointment.vaccines.service.AdminHospitalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * <p>
 * 医院信息
 * 服务实现类
 * </p>
 *
 * @author 王鑫浩
 * @since 2021-03-10
 */
@Service
public class AdminHospitalServiceImpl extends ServiceImpl<AdminHospitalMapper, AdminHospital> implements AdminHospitalService {

    @Override
    public Page<AdminHospital> selectPage(Page<AdminHospital> pageParam, AdminHospitalVo hospitalVo) {
        //条件构造器
        QueryWrapper<AdminHospital> wrapper = new QueryWrapper<>();
        //普通的排序分页查询
        if (hospitalVo == null) {
            return baseMapper.selectPage(pageParam, wrapper);
        }
        //获取对象参数
        String hospitalName = hospitalVo.getHospitalName();
        String province= hospitalVo.getProvince();
        String city=hospitalVo.getCity();
        String county=hospitalVo.getCounty();
        //条件

        if (!StringUtils.isEmpty(hospitalName)) {
            wrapper.likeRight("hospital_name", hospitalName);
        }
        if (!StringUtils.isEmpty(province)) {
            wrapper.likeRight("province", province);
        }
        if (!StringUtils.isEmpty(city)) {
            wrapper.likeRight("city", city);
        }
        if (!StringUtils.isEmpty(county)) {
            wrapper.likeRight("county", county);
        }

        return baseMapper.selectPage(pageParam, wrapper);
    }

    @Override
    public void saveExcel(MultipartFile file, AdminHospitalService hospitalService) {
        try {
            InputStream in = file.getInputStream();
            EasyExcel.read(in, AdminHospital.class, new HospitalListener(hospitalService)).sheet().doRead();
        } catch (Exception e) {
           throw new VaccineException(ResultCodeEnum.EXCEL_DATA_IMPORT_ERROR);
        }
    }

    @Override
    public String login(LoginVo loginVo) {
        QueryWrapper<AdminHospital> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",loginVo.getUsername());
        AdminHospital adminHospital=baseMapper.selectOne(queryWrapper);
        if (adminHospital== null){
            throw new VaccineException(ResultCodeEnum.LOGIN_ERROR);
        }

        //校验密码
        if (!MD5.encrypt(loginVo.getPassword()).equals(adminHospital.getPassword())) {
            throw new VaccineException(ResultCodeEnum.LOGIN_ERROR);
        }

        JwtInfo jwtInfo = new JwtInfo();
        jwtInfo.setId(adminHospital.getId());
        jwtInfo.setRoles("医院管理员");
        String jwtToken = JwtUtils.getJwtToken(jwtInfo, 12*60*60);

        return jwtToken;

    }
}
